﻿using BAL;
using DAL;
using DAL.DataTransferObject;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Module.QLNhaPhanPhoi
{
    public partial class frmQLNPP : Form
    {
        public NhaPhanPhoiBAO nppBAO = new NhaPhanPhoiBAO();

        public frmQLNPP()
        {
            InitializeComponent();
        }
        public void loadList()
        {
            List<NhaPhanPhoi> listNPP = nppBAO.NPP_GetData();
            DGV.DataSource = listNPP;
            for (int i = 0; i < listNPP.Count; i++)
            {
                DGV.Rows[i].Cells[0].Value = (i + 1);
            }
        }

        Timer timer = new Timer();
        private void frmQLNPP_Load(object sender, EventArgs e)
        {
            reset();
            loadList();
            DGV.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            int deWidth = 0;
            for (int i = 0; i < DGV.Columns.Count - 1; i++)
                deWidth += DGV.Columns[i].Width;
            DGV.Columns[DGV.Columns.Count - 1].Width = deWidth;
            timer.Interval = 1000;
            timer.Tick += timer_Tick;
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            btRefresh.PerformClick();
            timer.Stop();
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            reset();
        }
        public void reset()
        {
            txtMaNPP.Enabled = txtMaNPP.ReadOnly = true;
            txtTenNPP.Enabled = true;
            txtDiaChi.Enabled = true;
            txtSDT.Enabled = true;
            txtKyHD.Enabled = true;
            dtNgayBatDau.Enabled = true;
            dtNgayKetThuc.Enabled = true;
            //btSearchName.Enabled = true;
            btUpdate.Enabled = false;
            btDelete.Enabled = false;
            btAddNew.Enabled = true;

            txtMaNPP.Text = AutoGenCode.GenNewCode("NhaPhanPhoi");
            txtTenNPP.Clear();
            txtSDT.Clear();
            txtDiaChi.Clear();
            txtKyHD.Clear();

            dtNgayBatDau.Value = DateTime.Now;
            dtNgayKetThuc.Value = DateTime.Now;
            loadList();
        }

        public void getInfo(NhaPhanPhoi npp)
        {
            npp.MaNPP = txtMaNPP.Text;
            npp.TenNPP = txtTenNPP.Text;
            npp.DiaChi = txtDiaChi.Text;
            npp.SDT = txtSDT.Text;
            npp.NgayBatDauHD = Convert.ToDateTime(dtNgayBatDau.Value);
            npp.NgayKetThucHD = Convert.ToDateTime(dtNgayBatDau.Value);
            npp.LanKyHD = Convert.ToInt32(txtKyHD.Text);
        }

        private void DGV_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (SearchClicked)
                btSearchName.PerformClick();

            txtMaNPP.Text = DGV.CurrentRow.Cells[1].Value.ToString();
            txtTenNPP.Text = DGV.CurrentRow.Cells[2].Value.ToString();
            txtDiaChi.Text = DGV.CurrentRow.Cells[3].Value.ToString();
            txtSDT.Text = DGV.CurrentRow.Cells[4].Value.ToString();
            dtNgayBatDau.Value = Convert.ToDateTime(DGV.CurrentRow.Cells[5].Value);
            dtNgayKetThuc.Value = Convert.ToDateTime(DGV.CurrentRow.Cells[6].Value);
            txtKyHD.Text = DGV.CurrentRow.Cells[7].Value.ToString();

            btAddNew.Enabled = false;
            btDelete.Enabled = true;
            btUpdate.Enabled = true;
            //btSearchName.Enabled = false;
            txtMaNPP.Enabled = false;

            txtTenNPP.Enabled = true;
            txtDiaChi.Enabled = true;
            txtSDT.Enabled = true;
            txtKyHD.Enabled = true;
            dtNgayBatDau.Enabled = true;
            dtNgayKetThuc.Enabled = true;


        }

        private void btAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtMaNPP.Text.Length == 0) throw new Exception("Mã nhà phân phối không được bỏ trống");
                if (txtTenNPP.Text.Length == 0) throw new Exception("Tên nhà phân phối không được bỏ trống");
                if (txtDiaChi.Text.Length == 0) throw new Exception("Địa chỉ nhà phân phối không được bỏ trống");
                if (txtSDT.Text.Length == 0) throw new Exception("Số điện thoại nhà phân phối không được bỏ trống");
                if (txtKyHD.Text.Length == 0) txtKyHD.Text = "1";
                if (dtNgayBatDau.Value >= dtNgayKetThuc.Value) throw new Exception("Ngày bắt đầu phải trước ngày kết thúc");
                if (nppBAO.NPP_CheckName(txtTenNPP.Text)) throw new Exception("Trùng tên nhà phân phối!");
                NhaPhanPhoi npp = new NhaPhanPhoi();
                getInfo(npp);
                nppBAO.NPP_AddNew(npp);
                loadList();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR");
            }
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            NhaPhanPhoi npp = new NhaPhanPhoi();
            getInfo(npp);
            nppBAO.NPP_Update(npp);
            reset();
        }

        private void btDelete_Click(object sender, EventArgs e)
        {

            NhaPhanPhoi npp = new NhaPhanPhoi();
            getInfo(npp);
            nppBAO.NPP_Delete(npp);
            reset();
        }



        bool SearchClicked = false;
        DataView dv = new DataView();

        private void btSearchName_Click(object sender, EventArgs e)
        {
            if (!SearchClicked)
            {
                SearchClicked = true;
                txtDiaChi.Text = txtMaNPP.Text = txtSDT.Text = txtTenNPP.Text = txtKyHD.Text = "";
                txtMaNPP.ReadOnly = btAddNew.Enabled = btUpdate.Enabled = btDelete.Enabled = false;
                btSearchName.BackgroundImage = (Image)(Properties.Resources.ResourceManager.GetObject("TatTK"));
                btAddNew.Enabled = btRefresh.Enabled = false;
                dtNgayBatDau.Value = dtNgayBatDau.MinDate;
                dtNgayKetThuc.Value = dtNgayBatDau.MaxDate;
                dv = nppBAO.SelectViewAllNPP();
                DGV.DataSource = dv;
            }
            else
            {
                SearchClicked = false;
                btSearchName.BackgroundImage = (Image)(Properties.Resources.ResourceManager.GetObject("BatTK"));
                btRefresh.PerformClick();
                btAddNew.Enabled = btRefresh.Enabled = true;
                dtNgayKetThuc.Value = dtNgayBatDau.Value = DateTime.Now;
            }
        }


        //string stringFilter = "";
        List<string> listStringFilter = new List<string>();
        private void FilterChanged(object sender, EventArgs e)
        {
            if (SearchClicked)
            {
                listStringFilter = new List<string>();

                listStringFilter.Add(String.Format(" DiaChi like '{0}%'", txtDiaChi.Text));
                listStringFilter.Add(String.Format(" Convert(LanKyHD,System.String) like '{0}%'", txtKyHD.Text));
                listStringFilter.Add(String.Format(" MaNPP like '{0}%'", txtMaNPP.Text));
                listStringFilter.Add(String.Format(" SDT like '{0}%'", txtSDT.Text));
                listStringFilter.Add(String.Format(" TenNPP like '{0}%'", txtTenNPP.Text));

                listStringFilter.Add(String.Format("(BatDauHD >= '{0}' and KetThucHD <= '{1}')", dtNgayBatDau.Value, dtNgayKetThuc.Value));

                dv.RowFilter = ToStringFilter(listStringFilter);
                DGV.DataSource = dv;
            }

        }

        private string ToStringFilter(List<string> list)
        {
            Console.WriteLine(list.Count.ToString());
            string s = "";
            if (list.Count >= 2)
                for (int i = 0; i < list.Count - 1; i++)
                    s += list.ToArray()[i] + " AND ";
            s += list.ToArray()[list.Count - 1];
            return s;
        }


    }
}
